package gui;

import database.*;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.Timer;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;

public class PanelKokk extends JPanel implements ActionListener {
	private JScrollPane scrollPane;
	private DefaultTableModel tableModel, tableLages, tableFerdig;
	private JTable tabell;
	private JButton  buttonFerdiglaget, buttonHovedmeny;
	private JTextArea retterBestilt;
	private JLabel labelUlagede, labelRettene;
	private List<Bestilling> bestillinger;
	private Vector<String> kolonnenavn;
	private GridBagConstraints constraints;
	
	public PanelKokk(){
		this.setLayout(new GridBagLayout());
		this.leggTilKomponenter();
		GUI.getVindu().add(this);
		Timer t = new Timer(1000, this);
		t.setDelay(0);
		t.start();
		
	}
	
	private void leggTilKomponenter(){
		kolonnenavn = getKolonnenavn();
		tableModel = new DefaultTableModel(kolonnenavn,0){
			@Override
			public boolean isCellEditable(int row, int column) {
			       //all cells false
			       return false;
			}
		};
		
		tabell = new JTable(tableModel);
		tabell.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		ListSelectionModel rad = tabell.getSelectionModel();
		rad.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent e) {
				radSelected(e);
				
			}
		});
		fyllTableModel();
		tabell.getColumnModel().getColumn(0).setPreferredWidth(40);
		tabell.getColumnModel().getColumn(1).setPreferredWidth(250);
		tabell.getColumnModel().getColumn(2).setPreferredWidth(250);
		tabell.getColumnModel().getColumn(3).setPreferredWidth(80);
		
		
		buttonFerdiglaget = new JButton("Ferdiglaget");
		buttonFerdiglaget.addActionListener(this);
		buttonFerdiglaget.setEnabled(false);
		
		buttonHovedmeny = new JButton("Tilbake til hovedmeny");
		buttonHovedmeny.addActionListener(this);
		
		labelUlagede = new JLabel("Ulagede bestillinger");
		Font font = new Font(labelUlagede.getFont().getFontName(),labelUlagede.getFont().getStyle(),20);
		labelUlagede.setFont(font);
		
		labelRettene = new JLabel("Rettene:");
		labelRettene.setFont(font);
		
		scrollPane = new JScrollPane(tabell);
		scrollPane.setMinimumSize(new Dimension(800, 400));
		scrollPane.setPreferredSize(new Dimension(800, 400));
		
		retterBestilt = new JTextArea(1000, 1000);
		retterBestilt.setEditable(false);
		retterBestilt.setMinimumSize(new Dimension(300,150));
		font = new Font(retterBestilt.getFont().getFontName(),retterBestilt.getFont().getStyle(),15);
		retterBestilt.setFont(font);
		
		constraints = new GridBagConstraints();
		constraints.insets = new Insets(10, 10, 10, 10);
		constraints.gridx = 0;
		constraints.weightx = 1;
		constraints.gridwidth = 2;
		this.add(labelUlagede,constraints);
		constraints.gridy = 1;
		constraints.gridheight = 2;
		this.add(scrollPane,constraints);
		constraints.gridheight = 1;
		constraints.gridy = 3;
		constraints.gridwidth = 1;
		constraints.gridx = 0;
		constraints.anchor = GridBagConstraints.LINE_END;
		this.add(buttonFerdiglaget,constraints);
		constraints.gridx = 1;
		constraints.anchor = GridBagConstraints.LINE_START;
		this.add(buttonHovedmeny,constraints);
		constraints.gridx = 2;
		constraints.gridy = 1;
		this.add(labelRettene, constraints);
		constraints.gridy = 2;
		constraints.anchor = GridBagConstraints.NORTHWEST;
		this.add(retterBestilt, constraints);
	}

	private void fyllArea() {
		int[] rad = tabell.getSelectedRows();
		List<Rett> retter = Register.findRetter(bestillinger.get(rad[0]));
		String utskrift = "";
		for (int i = 0; i < retter.size(); i++) {
			if(i==retter.size()-1){
				utskrift += retter.get(i).getNavn();
			}
			else{
				utskrift += retter.get(i).getNavn() + "\n";				
			}	
		}
		retterBestilt.setText(utskrift);
		
	}
	private void buttonFerdiglaget_actionPerformed(ActionEvent e) 
	{
		int[] ferdige = tabell.getSelectedRows();
		Databasecon.setBestillingLaget(bestillinger.get(ferdige[0]));
		tableModel.removeRow(ferdige[0]);
	}
	private void buttonHovedmeny_actionPerformed(ActionEvent e) 
	{
		this.setVisible(false);
		new PanelHovedmeny();
	}

	private void radSelected(ListSelectionEvent e){
		
		if (tabell.getSelectedRows().length == 0) {
	        //No selection, disable fire button.
	            buttonFerdiglaget.setEnabled(false);

	        } else {
	        //Selection, enable the fire button.
	            buttonFerdiglaget.setEnabled(true);
	            fyllArea();
	        }
		
		
		
	}
	private Vector<String> getKolonnenavn(){
		Vector<String> kolonnenavn = new Vector<String>();
		kolonnenavn.add("BestillingsID");
		kolonnenavn.add("Retter");
		kolonnenavn.add("Annet");
		kolonnenavn.add("Leveringstidspunkt");
		return kolonnenavn;
	}
	private void fyllTableModel(){
		Register.oppdaterBestillinger();
		bestillinger = Register.findBestillingerKokk();
		for (int i = 0; i < bestillinger.size(); i++) {
			List<Rett> retter = Register.findRetter(bestillinger.get(i));
			String rettene = "";
			for (int j = 0; j < retter.size(); j++) {
				if(j==retter.size()-1){
					rettene += retter.get(j).getNavn();
				}
				else{
					rettene += retter.get(j).getNavn() + ", ";					
				}
			}
			Vector<String> radData = new Vector<String>();
			radData.add(Integer.toString(bestillinger.get(i).getBestillingsID()));
			radData.add(rettene);
			radData.add(bestillinger.get(i).getAnnet());
			radData.add(bestillinger.get(i).getLeveringstidspunkt());
			tableModel.addRow(radData);
		}
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonFerdiglaget)
			buttonFerdiglaget_actionPerformed(e);
		else if(e.getSource()==buttonHovedmeny)
			buttonHovedmeny_actionPerformed(e);
		else{
			new PanelKokk();
		}
		
		
	}
}
